﻿using System.Linq;
using System.Web.Mvc;
using Omu.AwesomeDemo.Core.Model;
using Omu.AwesomeDemo.Core.Repository;

namespace Omu.AwesomeDemo.WebUI.Controllers
{
    public class ShowAjaxRadioListController : Controller
    {
        private readonly IRepo<Show> r;

        public ShowAjaxRadioListController(IRepo<Show> r)
        {
            this.r = r;
        }

        public ActionResult GetItems(int? key)
        {
            var res = r.GetAll().Select(o => new SelectListItem { Text = o.Name, Value = o.Id.ToString(), Selected = (key == o.Id) });

            return Json(res);
        }
    }
    
    public class CharacterAjaxRadioListController : Controller
    {
        private readonly IRepo<Person> r;

        public CharacterAjaxRadioListController(IRepo<Person> r)
        {
            this.r = r;
        }

        public ActionResult GetItems(int? key, int? parent)
        {
            var res = r.GetAll().Where(o => o.Show != null && o.Show.Id == parent)
                .Select(o => new SelectListItem { Text = o.FirstName + " " + o.LastName, Value = o.Id.ToString(), Selected = (key == o.Id) });

            return Json(res);
        }
    }
    
    public class PersonHobbyAjaxRadioListController : Controller
    {
        private readonly IRepo<Person> r;

        public PersonHobbyAjaxRadioListController(IRepo<Person> r)
        {
            this.r = r;
        }

        public ActionResult GetItems(int? key, int? parent)
        {
            if (parent != null)
            {
                var res = r.Get(parent.Value).Hobbies
                    .Select(o => new SelectListItem { Text = o.Name, Value = o.Id.ToString(), Selected = (key == o.Id) });

                return Json(res);
            }
            return new EmptyResult();
        }
    }

    public class HobbyAjaxRadioListController : Controller
    {
        private readonly IRepo<Hobby> r;

        public HobbyAjaxRadioListController(IRepo<Hobby> r)
        {
            this.r = r;
        }

        public ActionResult GetItems(int? key)
        {
            var res = r.GetAll().Take(5).Select(o =>
                                        new SelectListItem
                                            {
                                                Text = o.Name,
                                                Value = o.Id.ToString(),
                                                Selected = (key == o.Id)
                                            });

            return Json(res);
        }
    }
}